package com.ruoyi;

import com.ruoyi.stock2.domain.STOCK;
import com.ruoyi.stock2.domain.TRADEDATA;
import com.ruoyi.stock2.mapper.STOCKMapper;
import com.ruoyi.stock2.service.IFasttextTrainService;
import com.ruoyi.stock2.service.ISelectModelFromPcaService;
import com.ruoyi.stock2.service.ITRADEDATAService;
import com.ruoyi.stock2.utils.DateUtils2;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ForeatchStocksToseq2seqTrain {
	private static final Logger log = LoggerFactory.getLogger(ForeatchStocksToseq2seqTrain.class);
	@Autowired(required = false)
	STOCKMapper stockMapper;
	@Autowired(required = false)
	IFasttextTrainService iFasttextTrainService;
	@Autowired(required = false)
	ITRADEDATAService itradedataService;
	@Autowired(required = false)
	ISelectModelFromPcaService iSelectModelFromPcaService;


	@Test
	public void buildseq2seqTrainToTxtforEatch() {
		List<STOCK> stocks = stockMapper.selectSTOCKList(null);
		for (int i = 0; i < stocks.size(); i++) {

			File file = new File("d:/seq2seq_train20200713/seq2seq_min"+stocks.get(i).getSymbol()+".txt");
			BufferedWriter writer = null;
			try {
				if(!file.exists()){
					file.createNewFile();
				}else{
					file.delete();
					file.createNewFile();
				}
				writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, false), "utf-8"));


				TRADEDATA tradedata = new TRADEDATA();
				tradedata.setSymbol(stocks.get(i).getSymbol());
				List<TRADEDATA> listinput = itradedataService.selectTRADEDATAList(tradedata);
				if(listinput.size()<1000){
					continue;
				}
				StringBuilder stringBuilderIt = new StringBuilder();


				List<BigDecimal> lista = new ArrayList<BigDecimal>(40);
				List<BigDecimal> listb = new ArrayList<BigDecimal>(40);
				for(int j = 0; j < listinput.size()-41; j++){

					for(int j1 = j; j1 < j+40; j1++){
						lista.add(listinput.get(j1).getCloseprice());
						listb.add(listinput.get(j1+1).getCloseprice());
					}

					BigDecimal mina = Collections.min(lista);
					BigDecimal minb = Collections.min(listb);

					stringBuilderIt.append(listinput.get(j).getSymbol()).append(",");
					stringBuilderIt.append(DateUtils2.formatDateToString(listinput.get(j).getDatetime(),"yyyyMMdd")).append(",");

					for(int k = 0; k<lista.size()-1; k++){
						stringBuilderIt.append(lista.get(k).divide(mina, 2, BigDecimal.ROUND_HALF_UP)).append(" ");
						//stringBuilderIt.append(lista.get(k)).append(" ");
					}
					stringBuilderIt.append(lista.get(lista.size()-1).divide(mina, 2, BigDecimal.ROUND_HALF_UP)).append(",");
					//stringBuilderIt.append(lista.get(lista.size()-1)).append(",");
					for(int k = 0; k<listb.size(); k++){
						//stringBuilderIt.append(listb.get(k)).append(" ");
						stringBuilderIt.append(listb.get(k).divide(minb, 2, BigDecimal.ROUND_HALF_UP)).append(" ");
					}
					writer.write(stringBuilderIt + "\r\n");
					writer.flush();
					lista.clear();
					listb.clear();
					stringBuilderIt.setLength(0);

				}
				writer.flush();
			} catch (IOException e1) {
				e1.printStackTrace();
			} finally {
				try {
					if (writer != null) {
						writer.close();
					}
				} catch (IOException  e) {
					e.printStackTrace();
				}
			}
		}
	}
}
